package aiball

import org.apache.jasper.compiler.Node.ParamsAction;
import org.springframework.dao.DataIntegrityViolationException;
import grails.plugins.springsecurity.Secured;
import grails.plugins.springsecurity.Secured;


class TeamController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def springSecurityService

	def index() {
		redirect(action: "list", params: params)
	}

	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def list() {

		[teamInstanceList: Team.find { id == ((UserAccount) springSecurityService.getCurrentUser()).getTeam().id } , teamInstanceTotal: Team.count() ]
	}
	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def save() {
		def teamInstance = new Team(params)
		teamInstance.setPlayer1(Player.get(1));
		teamInstance.setPlayer2(Player.get(1));
		teamInstance.setPlayer3(Player.get(1));
		teamInstance.setPlayer4(Player.get(1));
		teamInstance.setPlayer5(Player.get(1));

		Team alt=((UserAccount) springSecurityService.getCurrentUser()).getTeam();
		((UserAccount) springSecurityService.getCurrentUser()).setTeam(teamInstance);


		if(alt.id!=1){
			alt.delete();
		}

		if (!teamInstance.save(flush: true)) {
			render(view: "create", model: [teamInstance: teamInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'team.label', default: 'Team'),
			teamInstance.id
		])
		redirect(action: "show", id: teamInstance.id)
	}

	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def show() {
		def teamInstance;
		
		if(!springSecurityService.getPrincipal().getAuthorities().toString().equals("[ROLE_ADMIN]")){
			teamInstance =((UserAccount) springSecurityService.getCurrentUser()).getTeam();
			System.out.println("test");
		}
		else{
			teamInstance=Team.get(params.id);
		}
		if (!teamInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'team.label', default: 'Team'),
				params.id
			])
			redirect(action: "list")
			return
		}
		def gameList = Gamecharts.findAllByTeam2Like(((UserAccount) springSecurityService.getCurrentUser()).getTeam());
		gameList.addAll(Gamecharts.findAllByTeam1Like(((UserAccount) springSecurityService.getCurrentUser()).getTeam()))
		[teamInstance: teamInstance, gameInstanceList: gameList]
	}

	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def showOtherTeam() {
		def teamInstance =Team.get(params.id)

		if (!teamInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'team.label', default: 'Team'),
				params.id
			])
			redirect(action: "list")
			return
		}
		def gameList = Gamecharts.findAllByTeam2Like(Team.get(params.id))
		gameList.addAll(Gamecharts.findAllByTeam1Like(Team.get(params.id)))
		gameList.removeAll(Gamecharts.findAllByTeam1Goals(-1))
		[teamInstance: teamInstance, gameInstanceList: gameList]
	}

	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def edit() {
		def teamInstance = null;
		if(!springSecurityService.getPrincipal().getAuthorities().toString().equals("[ROLE_ADMIN]")){
			teamInstance =((UserAccount) springSecurityService.getCurrentUser()).getTeam();
		}
		else{
			teamInstance=Team.get(params.id);
		}
		
		if (!teamInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'team.label', default: 'Team'),
				params.id
			])
			redirect(action: "list")
			return
		}
		[teamInstance: teamInstance]
	}

	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def update() {
		def teamInstance = ((UserAccount) springSecurityService.getCurrentUser()).getTeam()
		def teamnamealt=Team.executeQuery("select name from Team where id="+teamInstance.id)
		if (!teamInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'team.label', default: 'Team'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (teamInstance.version > version) {
				teamInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
				[
					message(code: 'team.label', default: 'Team')]
				as Object[],
				"Another user has updated this Team while you were editing")
				render(view: "edit", model: [teamInstance: teamInstance])
				return
			}
		}

		teamInstance.properties = params

		if (!teamInstance.save(flush: true)) {
			render(view: "edit", model: [teamInstance: teamInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'team.label', default: 'Team'),
			teamInstance.id
		])
		if(!teamnamealt.toString().substring(1,teamnamealt.toString().size()-1).equals(teamInstance.name)){
			File file=new File("/var/www/web1/javakicker/teams/"+teamnamealt.toString().substring(1,teamnamealt.toString().size()-1));
			if(file.exists()){

				file.renameTo(new File("/var/www/web1/javakicker/teams/"+teamInstance.name));
				def comp=new CompilerService();
				comp.generateLineupXML(teamInstance);
			}
		}

		redirect(action: "show", id: teamInstance.id)
	}
}
